<template>
  <div class="app-container">
    <label class="title" style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">考试设备</label>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="名称或地址：" prop="main">
        <el-input
          v-model="queryParams.main"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备分组：" prop="group">
        <el-input
          v-model="queryParams.group"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用状态：" prop="useStatus">
        <el-select v-model="queryParams.useStatus" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in dict.type.useStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleEdit"
          v-hasPermi="['business:examVest:edit']"
        >按分组启用/停用
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
          v-hasPermi="['business:examVest:upload']"
        >上传文件
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportModel"
          v-hasPermi="['business:examVest:exportModel']"
        >文件模版
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:examVest:export']"
        >设备导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="devList" >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index+(queryParams.pageNo-1)*queryParams.pageSize+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="设备分组" align="center" prop="group" />
      <el-table-column label="启用状态" align="center" prop="useStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.useStatus" :value="scope.row.useStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="在线状态" align="center" prop="online" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isOnline" :value="scope.row.online"/>
        </template>
      </el-table-column>
      <el-table-column label="限制性别" align="center" prop="gender" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="typeStr" />
      <el-table-column label="验证模式" align="center" prop="verifyMode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.verifyMode" :value="scope.row.verifyMode"/>
        </template>
      </el-table-column>
      <el-table-column label="计分模式" align="center" prop="resultMode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.resultMode" :value="scope.row.resultMode"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:dev:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:dev:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="考生版本" prop="candidateVer">
          <el-input v-model="form.candidateVer" placeholder="请输入考生版本" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建时间" prop="gmtCreated">
          <el-date-picker clearable size="small"
                          v-model="form.gmtCreated"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="gmtModified">
          <el-date-picker clearable size="small"
                          v-model="form.gmtModified"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="验证模式" prop="verifyMode">
          <el-input v-model="form.verifyMode" placeholder="请输入验证模式" />
        </el-form-item>
        <el-form-item label="成绩模式" prop="resultMode">
          <el-input v-model="form.resultMode" placeholder="请输入成绩模式" />
        </el-form-item>
        <el-form-item label="xb-性别1男2女" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入xb-性别1男2女" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 文件上传对话框 -->
    <el-dialog title="上传考试设备" :visible.sync="uploadOpen" width="500px" append-to-body>
      <el-col :sm="24" align="middle">
        <el-upload id="importUpload"
                   class="upload-demo"
                   drag
                   :show-file-list=false
                   :on-success="uploadSuccess"
                   :on-error="uploadError"
                   :action="uploadUrl">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">点击这里或拖放文件到这里上传</div>
        </el-upload>
      </el-col>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel2">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 按分组启停对话框 -->
    <el-dialog title="按组启用/停用" :visible.sync="groupOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules2" label-width="100px">
        <el-form-item label="分组：" prop="group">
          <el-select v-model="form.group" placeholder="请选择" clearable size="small" class="selectWidth">
            <!--<el-option
              v-for="dict in groupDevs"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>-->
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态：" prop="useStatus">
          <el-select v-model="form.useStatus" placeholder="请选择" clearable size="small" class="selectWidth">
            <el-option
              v-for="dict in dict.type.useStatus"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel3">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listDev, delDev, exportDev, exportModel, groupDevs, importDevs,switchUseStatusForGroup,switchUseStatus,updatePart,updateDevs,getDev } from "@/api/business/examDev/examDev";

    export default {
        name: "Dev",
        dicts: ['useStatus',"sys_user_sex","isOnline","resultMode","verifyMode"],
        data() {
            return {
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 考试设备表格数据
                devList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                uploadOpen: false,
                groupOpen:false,
                // 查询参数
                queryParams: {
                    pageNo: 1,
                    pageSize: 10,
                    main: "",
                    group: "",
                    useStatus: ""
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    version: [
                        { required: true, message: "版本不能为空", trigger: "blur" }
                    ]
                },
                rules2:{
                    group: [
                        { required: true, message: "分组必须选择", trigger: "blur" }
                    ],
                    useStatus: [
                        { required: true, message: "启用状态", trigger: "blur" }
                    ]
                },
                uploadUrl: '/' + this.$store.state.settings.prefix + '/business/examDevs/import',
                groupDevs: []
            };
        },
        created() {
            this.getList();
            groupDevs().then(response => {
                this.groupDevs = response.data;
            });
        },
        methods: {
            /** 查询考试设备列表 */
            getList() {
                this.loading = true;
                listDev(this.queryParams).then(response => {
                    const records = response.data.records;
                    this.devList = records;
                    for (var i = 0; i <= records.length; i = i + 1) {
                        if (records[i] == null) {
                            break
                        }
                        this.devList[i].online = records[i].online+'';
                    }
                    this.total = response.data.totalCount;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 关闭按钮
            cancel2() {
                this.uploadOpen = false;
                this.reset();
            },
            // 关闭按钮
            cancel3() {
                this.groupOpen = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    candidateVer: null,
                    remark: null,
                    gmtCreated: null,
                    gmtModified: null,
                    isDeleted: null,
                    verifyMode: null,
                    resultMode: null,
                    gender: null,
                    version: null,
                    group:null,
                    useStatus:""
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNo = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加考试设备";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                getDev(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改考试设备";
                });
            },
            /** 启停按钮操作 */
            handleEdit(row) {
                this.reset();
                const id = row.id || this.ids;
                getDev(id).then(response => {
                    this.form = response.data;
                    /*this.form.useStatus = response.data.useStatus+'';*/
                    this.groupOpen = true;
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        switchUseStatusForGroup(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.groupOpen = false;
                            this.getList();
                        });
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除考试设备编号为"' + ids + '"的数据项？').then(function() {
                    return delDev(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            },

            /** 上传考试设备按钮 */
            handleUpload() {
                this.uploadOpen = true
            },
            /** 下载文件模版 */
            handleExportModel() {
                window.open('/' + this.$store.state.settings.prefix + '/business/examDevs/exportModel')
            },
            /** 考试设备导出 */
            handleExport() {
                window.open('/' + this.$store.state.settings.prefix + '/business/examDevs/export')
            },
            //文件上传异常结果
            uploadError(err, file, fileList) {
                this.$modal.msgSuccess('文件导入失败，失败原因：' + err.msg)
            },
            //文件上传成功结果
            uploadSuccess(response, file, fileList) {
                if (response.status != 'success') {
                    this.$modal.msgSuccess('文件导入失败,返回原因：' + response.msg);
                    return
                }
                this.$modal.msgSuccess('上传成功')
            }
        }
    };
</script>
